﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorkbenchTest
{
    public partial class Editor : Form
    {
        EditorType editorType;

        public Editor(EditorType et)
        {
            InitializeComponent();
            LoadWithEditor(et);
        }

        private void LoadWithEditor(EditorType et)
        {
            editorType = et;
            dataGridView1.ColumnCount = et.Selectors.Count;

            for (int i = 0; i < et.Selectors.Count; ++i)
            {
                dataGridView1.Columns[i].HeaderText = et.Selectors[i].ColumnHeader;
                dataGridView1.Columns[i].ReadOnly = et.Selectors[i].Readonly;
            }
        }

        private void Editor_Load(object sender, EventArgs e)
        {

        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            List<List<string>> rows = new List<List<string>>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Index == dataGridView1.Rows.Count - 1)
                    break;

                List<string> cells = new List<string>();

                foreach (DataGridViewCell cell in row.Cells)
                {
                    cells.Add(cell.Value as string);
                }

                rows.Add(cells);
            }

            var newRows = editorType.Apply(rows);
            dataGridView1.Rows.Clear();
            
            foreach (var row in newRows)
                dataGridView1.Rows.Add(row.ToArray());
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
                return;

            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value =
                editorType.Selectors[e.ColumnIndex].Select(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonGetAll_Click(object sender, EventArgs e)
        {
            var allRows = editorType.GetAll();

            dataGridView1.Rows.Clear();

            foreach (var row in allRows)
                dataGridView1.Rows.Add(row.ToArray());
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            List<List<string>> selectedRows = new List<List<string>>();

            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                if (row.Index == dataGridView1.Rows.Count - 1)
                    break;

                List<string> cells = new List<string>();

                foreach (DataGridViewCell cell in row.Cells)
                {
                    cells.Add(cell.Value as string);
                }

                selectedRows.Add(cells);
            }

            editorType.Delete(selectedRows);

            foreach (var selectedRow in dataGridView1.SelectedRows)
                dataGridView1.Rows.Remove(selectedRow as DataGridViewRow);
        }
    }
}
